package cn.lele.reggie.service.impl;

import cn.lele.reggie.dto.DishDto;
import cn.lele.reggie.entity.Dish;
import cn.lele.reggie.entity.DishFlavor;
import cn.lele.reggie.mapper.DishMapper;
import cn.lele.reggie.service.CategoryService;
import cn.lele.reggie.service.DishFlavorService;
import cn.lele.reggie.service.DishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
 public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public List<DishDto> listDish(Long categoryId,Integer status) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,categoryId)
                .orderByDesc(Dish::getUpdateTime)
                .eq(status!=null,Dish::getStatus,status);
        List<Dish> dishList = this.list(wrapper);
        log.info("dishlist:"+dishList);
        List<DishDto> dishDtoArrayList = new ArrayList<>();
        //加到dto去
        for (Dish dish : dishList) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish,dishDto);
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
            dishDto.setFlavors(dishFlavorService.list(dishFlavorLambdaQueryWrapper));
            dishDtoArrayList.add(dishDto);
        }
        log.info("dishdto---->"+dishDtoArrayList);
        return dishDtoArrayList;
    }

    @Override
    @Transactional
    public void saveDish(DishDto dish) {
        //将dto的部分属性添加到dish表
        this.save(dish);
        //将flavour添加到dish_flavour表
        List<DishFlavor> flavors = dish.getFlavors();
        Long dishId = dish.getId();
        //将flavours中的数据转为DishFlavor对象
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
            dishFlavorService.save(flavor);
        }
        redisTemplate.delete("Dish_"+dish.getCategoryId());
    }

    @Override
    public Page<DishDto> pageDishAndflavour(Integer page, Integer pageSize, String name) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name!=null,Dish::getName,name);
        wrapper.orderByDesc(Dish::getUpdateTime);
        Page<Dish> dishPage = new Page<>(page,pageSize);
        Page<DishDto> dishDtoPage = new Page<>(page,pageSize);
        this.page(dishPage,wrapper);
        //将dish中的数据拷贝到dishdto，除了records字段，因为要将里面的对象加工
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");
        ArrayList<DishDto> dtoArrayList = new ArrayList<>();
        //对didhsto加工
        for (Dish dish : dishPage.getRecords()) {
            Long categoryId = dish.getCategoryId();
            //获取分类名称
            String categoryName = categoryService.getById(categoryId).getName();
            DishDto dishDto = new DishDto();
            //将原来dish的数据拷贝到dto
            BeanUtils.copyProperties(dish,dishDto);
            dishDto.setCategoryName(categoryName);
            dtoArrayList.add(dishDto);
        }
        dishDtoPage.setRecords(dtoArrayList);
        return dishDtoPage;
    }

    @Override
    public DishDto getDishDtoById(Long id) {
        Dish dish = this.getById(id);
        //根据这个dishId查这个菜品所有的口味
        LambdaQueryWrapper<DishFlavor> flavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        flavorLambdaQueryWrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> dishFlavorList = dishFlavorService.list(flavorLambdaQueryWrapper);
        log.info("口味list--->"+dishFlavorList);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(dishFlavorList);
        return dishDto;
    }

    @Override
    @Transactional
    public void updateDishAndFlavour(DishDto dishDto) {
        //更新菜品
        this.updateById(dishDto);
        //不能直接更新口味，因为可能新加入，就不会分配DishFlavourId
        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        //更新菜品口味（即添加）
        for (DishFlavor flavor : dishDto.getFlavors()) {
            flavor.setDishId(dishDto.getId());
            dishFlavorService.save(flavor);
        }
        redisTemplate.delete("Dish_"+dishDto.getCategoryId());
    }

    @Override
    public void updateStatus(Integer statusId, Long[] ids) {
        //redis中的key
        String key=null;
        //改为禁止销售
        if(statusId==0){
            log.info("要禁止了。。。。。。。。。。。。。");
            for (Long id : ids) {
                Dish dish = this.getById(id);
                dish.setStatus(0);
                this.updateById(dish);
                key="Dish_"+dish.getCategoryId().toString();
                //删除redis中的数据
                redisTemplate.delete(key);
            }
        }else{
            log.info("要开始售卖了。。。。。。。。。。。。。");
            for (Long id : ids) {
                Dish dish = this.getById(id);
                dish.setStatus(1);
                this.updateById(dish);
                key="Dish_"+dish.getCategoryId().toString();
                //删除redis中的数据
                redisTemplate.delete(key);
            }
        }

    }

    @Override
    @Transactional
    public void deleteWithFlavour(Long[] ids) {
        //id是菜品的id
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        for (Long id : ids) {
            Dish dish = getById(id);
            this.removeById(id);
            //删除套餐中的菜品
            wrapper.eq(DishFlavor::getDishId,id);
            dishFlavorService.remove(wrapper);
            //删除redis中的数据
            redisTemplate.delete("Dish_"+dish.getCategoryId());
        }
    }
}
